package com.ht.web360.base.dto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;
import java.util.function.Function;

@Data
@Schema(description = "返回页面参数封装")
public class RPage<T> {
    @Schema(description = "当页的数据")
    List<T> records;

    @Schema(description = "所有数据数量")
    Long total;

    @Schema(description = "当前页面大小")
    Long size;

    @Schema(description = "当前页码")
    Long current;

    @Schema(description = "页面总数")
    Long pages;

    public static <T> RPage<T> getInstance(RPage<T> page) {
        RPage<T> res = new RPage<>();
        res.total = page.getTotal();
        res.size = page.getSize();
        res.current = page.getCurrent();
        res.pages = page.getPages();
        res.records = page.getRecords();
        return res;
    }

    public static <T> RPage<T> getInstance(IPage<T> page) {
        RPage<T> res = new RPage<>();
        res.total = page.getTotal();
        res.size = page.getSize();
        res.current = page.getCurrent();
        res.pages = page.getPages();
        res.records = page.getRecords();
        return res;
    }

    public static <R, T> RPage<R> getInstance(IPage<T> page, Function<T, R> convertor) {
        RPage<R> res = new RPage<>();
        res.total = page.getTotal();
        res.size = page.getSize();
        res.current = page.getCurrent();
        res.pages = page.getPages();
        res.records = page.getRecords().stream().map(convertor).toList();
        return res;
    }

    public static <R, T> RPage<R> getInstance(RPage<T> page, Function<T, R> convertor) {
        RPage<R> res = new RPage<>();
        res.total = page.getTotal();
        res.size = page.getSize();
        res.current = page.getCurrent();
        res.pages = page.getPages();
        res.records = page.getRecords().stream().map(convertor).toList();
        return res;
    }
}
